#!/bin/bash

# Script for building in a Docker container on Travis.

set -e  # Exit on error.

export P4C_PYTHON3="python3" # Python3 is required for p4c to run, P4C_DEPS would otherwise uninstall it

export P4C_DEPS="bison \
             build-essential \
             cmake \
             curl \
             flex \
             g++ \
             libboost-dev \
             libboost-graph-dev \
             libboost-iostreams1.58-dev \
             libfl-dev \
             libgc-dev \
             libgmp-dev \
             pkg-config \
             python \
             python3-pip \
             python3-setuptools \
             tcpdump"

export P4C_EBPF_DEPS="libpcap-dev \
             libelf-dev \
             zlib1g-dev \
             llvm \
             clang \
             net-tools"

export P4C_RUNTIME_DEPS="cpp \
                     libboost-graph1.58.0 \
                     libboost-iostreams1.58.0 \
                     libgc1c2 \
                     libgmp10 \
                     libgmpxx4ldbl \
                     python3"

export P4C_PIP_PACKAGES="ipaddr \
                     pyroute2 \
                     ply==3.8 \
                     scapy==2.4.0"

export P4C_PIP3_PACKAGES="ipaddr \
                          pyroute2 \
                          ply==3.8 \
                          scapy"

apt-get update
apt-get install -y --no-install-recommends \
  ${P4C_DEPS} \
  ${P4C_PYTHON3} \
  ${P4C_EBPF_DEPS} \
  ${P4C_RUNTIME_DEPS} \
  git

pip3 install wheel
pip3 install $P4C_PIP3_PACKAGES

# Build libbpf for eBPF tests.
cd /p4c
python3 backends/ebpf/build_libbpf
cd -

# We also need to build iproute2 from source to support Ubuntu 16.04 eBPF.
cd /tmp
git clone -b v5.0.0 git://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git
cd /tmp/iproute2
./configure
make -j `getconf _NPROCESSORS_ONLN` && \
make install
cd /p4c
rm -rf /tmp/pip
# iproute2-end


# ! ------- BEGIN Python2 -------------------------------------------------
# ! ------- Should be deleted when conversion to Python3 is finalized -----
# We install pip with get-pip.py (https://pip.pypa.io/en/stable/installing/)
# since the Ubuntu package manager's version of pip seems to be broken on
# Ubuntu 16.04.
mkdir /tmp/pip
cd /tmp/pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python2 get-pip.py
cd -
rm -rf /tmp/pip

# just to make sure we don't break other downstream projects that have
# not yet converted, we still install python2 and the modules
pip install $P4C_PIP_PACKAGES
# ! ------  END Python2 ----------------------------------------------------

# ! ------  BEGIN VALIDATION -----------------------------------------------
# These steps are necessary to validate the correct compilation of the P4C test
# suite programs. See also https://github.com/p4gauntlet/p4_tv.

# Also pull Gauntlet for translation validation
git clone -b stable https://github.com/p4gauntlet/p4_tv /gauntlet
cd /gauntlet
git submodule update --init --recursive
cd -
# Symlink the parent p4c repository with Gauntlet
rm -rf /gauntlet/modules/p4c
ln -s /p4c /gauntlet/modules/p4c

# Symlink the toz3 extension for the p4 compiler
mkdir -p /p4c/extensions
ln -sf /gauntlet/modules/toz3 /p4c/extensions/toz3

# Install Gauntlet Python dependencies locally
# Unfortunately we need Python 3.6, which Xenial does not have
add-apt-repository ppa:deadsnakes/ppa
apt update
apt install -y python3.6
python3.6 -m pip install z3-solver
python3.6 -m pip install pytest
# to parallelize tests
python3.6 -m pip install pytest-xdist
python3.6 -m pip install dataclasses
# ! ------  END VALIDATION ------------------------------------------------

function build() {
  if [ -e build ]; then /bin/rm -rf build; fi
  mkdir -p build
  cd build

  cmake .. '-DCMAKE_CXX_FLAGS:STRING=-O3' "$@"
  make
}

build "-DENABLE_UNIFIED_COMPILATION=${ENABLE_UNIFIED_COMPILATION}"

make install
/usr/local/bin/ccache -p -s

if [[ "${IMAGE_TYPE}" == "build" ]] ; then
  apt-get purge -y ${P4C_DEPS} git
  apt-get autoremove --purge -y
  rm -rf /p4c /var/cache/apt/* /var/lib/apt/lists/*
  echo 'Build image ready'

elif [[ "${IMAGE_TYPE}" == "test" ]] ; then
  echo 'Test image ready'

fi
